package HLLib.ui;

import HLCode.HLClass;
import HLCode.HLClassManager;
import HLCode.HLClassManager_H;
import HLCode.HLObject;
import HLLib.base.HLGraphics;
import HLLib.base.HLGraphics_H;
import HLLib.base.HLImage;
import HLLib.base.HLRectangle;
import HLLib.io.HLFile;

public class HLUIControlImage extends HLUIControl implements HLGraphics_H {
    public HLClass GetClass(HLClassManager classManager) {
        return classManager.GetLibClass(HLClassManager_H.CLASS_GROUP_TYPE_LIB_UI, HLClassManager_H.LIB_CLASS_UI_UIControlImage);
    }

    public int GetInt(int index) {
        switch (index) {
        case 1: {
            return imageTrans;
        }
        case 2: {
            return imagePosStyle;
        }
        }
        return super.GetInt(index);
    }

    public void SetInt(int index, int value) {
        switch (index) {
        case 1: {
            imageTrans = (byte) value;
            return;
        }
        case 2: {
            imagePosStyle = (byte) value;
            return;
        }
        }
        super.SetInt(index, value);
    }

    public boolean GetBoolean(int index) {
        return super.GetBoolean(index);
    }

    public void SetBoolean(int index, boolean value) {
        super.SetBoolean(index, value);
    }

    public HLObject GetObject(int index) {
        switch (index) {
        case 0: {
            return image;
        }
        }
        return super.GetObject(index);
    }

    public void SetObject(int index, HLObject value) {
        switch (index) {
        case 0: {
            image = (HLImage) value;
            return;
        }
        }
        super.SetObject(index, value);
    }

    public byte imageTrans;

    public byte imagePosStyle;

    public HLImage image;

    public HLUIControlImage() {
    }

    HLUIControlImage(HLUIFrame ownerFrame) {
        super(ownerFrame);
    }

    void Load(HLFile file) {
        super.Load(file);

        byte packageIndex = file.ReadInt8();
        short imageIndex = file.ReadInt16();

        imageTrans = file.ReadInt8();
        imagePosStyle = file.ReadInt8();

        image = ownerFrame.owner.ownerUISeedManager.imageManager.GetImageEX(packageIndex, imageIndex, false);
    }

    void Render(HLGraphics g, int rectX, int rectY, int rectWidth, int rectHeight, int color) {
        HLRectangle area = GetArea(rectX, rectY, rectWidth, rectHeight);

        int tempWidth;
        int tempHeight;
        if (imageTrans < 4) {
            tempWidth = image.GetWidth();
            tempHeight = image.GetHeight();
        } else {
            tempWidth = image.GetHeight();
            tempHeight = image.GetWidth();
        }

        int centerX = area.x + ((imagePosStyle & LEFT) != 0 ? tempWidth >> 1 : //
                (imagePosStyle & RIGHT) != 0 ? area.width - tempWidth + (tempWidth >> 1) : //
                        area.width >> 1);

        int centerY = area.y + ((imagePosStyle & TOP) != 0 ? tempHeight >> 1 : //
                (imagePosStyle & BOTTOM) != 0 ? area.height - tempHeight + (tempHeight >> 1) : //
                        area.height >> 1);

        g.DrawImageEX1(image, centerX, centerY, imageTrans, 10000, 10000, color, 0, 0, 0);
    }
    

    void CopyFrom(HLUIControl uiControl) {
        super.CopyFrom(uiControl);

        HLUIControlImage uiControlByType = (HLUIControlImage) uiControl;

        imageTrans = uiControlByType.imageTrans;

        imagePosStyle = uiControlByType.imagePosStyle;

        image = uiControlByType.image;
    }

    HLUIControl Clone(HLUIFrame ownerFrame) {
        HLUIControlImage uiControl = new HLUIControlImage();
        uiControl.ownerFrame = ownerFrame;
        uiControl.CopyFrom(this);
        return uiControl;
    }
}
